#include "stdafx.h"
#include "GPR.h"
#include "TCR.h"

#include <log4cplus/logger.h>
#include <log4cplus/FileAppender.h>

using namespace log4cplus;
using namespace log4cplus::helpers;

static log4cplus::Logger& GetLogger()
{
	static bool isCreated = false;
	static log4cplus::Logger logger = log4cplus::Logger::getInstance(_T("LOG"));
	if (!isCreated)
	{
		char exeName[MAX_PATH] = "";
		::GetModuleFileName(NULL,exeName,1024);
	
		SYSTEMTIME st = {0};
		::GetLocalTime(&st);
		char logFile[1024] = "";
		sprintf_s(logFile,"%s-%02d-%02d.slg",exeName,st.wMonth,st.wDay);

		SharedAppenderPtr appender(new RollingFileAppender(logFile));
		appender->setName(_T("LOG"));

		log4cplus::tstring pattern = _T("%-5p %D{%H:%M:%S.%q}[%t]:`%m`%n");
		std::auto_ptr<Layout> layout(new PatternLayout(pattern));
		appender->setLayout(layout);

		logger.addAppender(appender);
		isCreated = true;
	}
	return logger;
}

static HighPerformanceTimer s_logTimer;
void Log::Debug(char const* msg)
{
	FT formatter("MS[%I64d] %s", s_logTimer.GetUnitTick(MICRO_SECOND), msg);
#ifdef NDEBUG
	char buff[8192] = "";
	LOG4CPLUS_DEBUG(GetLogger(), Encryption::Encrypt(formatter,buff,4096));
#else
	LOG4CPLUS_DEBUG(GetLogger(), formatter);
#endif
}

void Log::Info(char const* msg)
{
	FT formatter("MS[%I64d] %s", s_logTimer.GetUnitTick(MICRO_SECOND), msg);
#ifdef NDEBUG
	char buff[8192] = "";
	LOG4CPLUS_INFO(GetLogger(), Encryption::Encrypt(formatter,buff,4096));
#else
	LOG4CPLUS_INFO(GetLogger(), formatter);
#endif
}

void Log::Warn(char const* msg)
{
	FT formatter("MS[%I64d] %s", s_logTimer.GetUnitTick(MICRO_SECOND), msg);
#ifdef NDEBUG
	char buff[8192] = "";
	LOG4CPLUS_WARN(GetLogger(), Encryption::Encrypt(formatter,buff,4096));
#else
	LOG4CPLUS_WARN(GetLogger(), formatter);
#endif
}

void Log::Error(char const* msg)
{
	FT formatter("MS[%I64d] %s", s_logTimer.GetUnitTick(MICRO_SECOND), msg);
#ifdef NDEBUG
	char buff[8192] = "";
	LOG4CPLUS_ERROR(GetLogger(), Encryption::Encrypt(formatter,buff,4096));
#else
	LOG4CPLUS_ERROR(GetLogger(), formatter);
#endif
}

Profile::Profile()
{
	char path[MAX_PATH] = "";
	::GetModuleFileName(NULL, path, 1024);

	m_fileName = path;
	string::size_type findPos = m_fileName.find_last_of('.');
	m_fileName = m_fileName.erase(findPos+1) + "ini";
}

string Profile::GetPrivateProfile(char const* appName, char const* keyName, char const* defaultValue)
{
	assert(!m_fileName.empty());
	char tmpBuf[512] = "";
	::GetPrivateProfileString(appName, keyName, defaultValue, tmpBuf, sizeof(tmpBuf), m_fileName.c_str());
	return tmpBuf;
}

int Profile::GetPrivateProfile(char const* appName, char const* keyName, int defaultValue)
{
	assert(!m_fileName.empty());
	return ::GetPrivateProfileInt(appName, keyName, defaultValue, m_fileName.c_str());
}

void Profile::WritePrivateProfile(char const* appName, char const* keyName, char const* value)
{
	assert(!m_fileName.empty());
	::WritePrivateProfileString(appName, keyName, value, m_fileName.c_str());
}

void Profile::WritePrivateProfile(char const* appName, char const*keyName, int value)
{
	assert(!m_fileName.empty());
	char tmpBuf[256] = "";
	sprintf_s(tmpBuf,"%d",value);
	::WritePrivateProfileString(appName, keyName, tmpBuf, m_fileName.c_str());
}

Profile& theProfile()
{
	static Profile obj;
	return obj;
}

int StringSplit::GetByDelimiter(char* originalStr, char const* delimit, vector<string>& destVector)
{
	assert((NULL != originalStr) && (NULL != delimit));	
	destVector.clear();

	char* firstToken = NULL;
	char* nextToken = NULL;
	firstToken = strtok_s(originalStr, delimit, &nextToken);
	while (NULL != firstToken)
	{
		string tokenStr = firstToken;
		destVector.push_back(tokenStr);
		firstToken = strtok_s(NULL, delimit, &nextToken);
	}

	return destVector.size();
}

StringSplit& theStringSplit()
{
	static StringSplit obj;
	return obj;
}